using UnityEngine;
using QFramework;

namespace XCZProject
{
	public partial class EnemyMiniBoss : ViewController,IEnemy
	{
		/// <summary>
		/// BOSS的状态
		/// </summary>
		public enum States
		{
			FlowingPlayer,//跟随主角
			Waring,//警戒状态
			Dash,//冲向主角
			Wait,//等待状态
		}

		public FSM<States> FSM = new FSM<States>();
		
		public float HP = 50;
		
		public float MovementSpeed = 2.0f;
		void Start()
		{
			EnemyGenerator.EnemyCount.Value++;

			// 把状态都添加进FSM里面
			FSM.State(States.FlowingPlayer)
				.OnFixedUpdate(() =>
				{
					if (Player.Default)
					{	
						// 获取玩家当前方向
						var direction = (Player.Default.transform.position - transform.position).normalized;
			
						// 向玩家方向移动
						//transform.Translate(direction*Time.fixedDeltaTime*MovementSpeed);
						// 用刚体触发伤害事件，避免用上面这个代码，容易在接触到敌人时触发多次伤害
						SelfRigidbody2D.velocity = direction * MovementSpeed;

						// 当boss和玩家距离小于15，则进入警戒状态
						if ((Player.Default.transform.Position()-transform.Position()).magnitude<=15)
						{
							FSM.ChangeState(States.Waring);
						}
					}
					else
					{
						// 如果没有主角信息，则待机
						SelfRigidbody2D.velocity = Vector2.zero;
					}
				});
			FSM.State(States.Waring)
				.OnEnter(() =>
				{
					// 进入警戒状态时2，boss站定不懂，进入观察
					SelfRigidbody2D.velocity=Vector2.zero;
				})
				.OnUpdate(() =>
				{
					// boss前摇
					var frames = 3 + (60 * 3 - FSM.FrameCountOfCurrentState) / 10;

					if (FSM.FrameCountOfCurrentState/frames%2==0)
					{
						Sprite.color=Color.red;
					}
					else
					{
						Sprite.color=Color.white;
					}
					
					// 正常情况下需要等待X秒后冲向主角，fsm中自带一个函数，计算多少帧，一般60帧为1秒，所以60帧后改变状态
					if (FSM.FrameCountOfCurrentState>=60*3)
					{
						FSM.ChangeState(States.Dash);
					}
				})
				.OnExit(() =>
				{
					// 当退出状态时，boss颜色重置为白色
					Sprite.color=Color.white;
				});

			var dashStartPos = Vector3.zero;
			var dashStartDistanceToPlayer = 0f;
			FSM.State(States.Dash)
				.OnEnter(() =>
				{
					var direction = (Player.Default.Position() - transform.Position()).normalized;
					SelfRigidbody2D.velocity = direction * 15;

					dashStartPos = transform.Position();
					// boss和玩家的距离
					dashStartDistanceToPlayer = (Player.Default.Position() - transform.Position()).magnitude;
				})
				.OnUpdate(() =>
				{
					var distance = (transform.Position() - dashStartPos).magnitude;
					if (distance>=dashStartDistanceToPlayer+5f)
					{
						FSM.ChangeState(States.Wait);
					}
				});

			FSM.State(States.Wait)
				.OnEnter(() =>
				{
					SelfRigidbody2D.velocity = Vector2.zero;
				})
				.OnUpdate(() =>
				{
					if (FSM.FrameCountOfCurrentState >= 720)
					{
						FSM.ChangeState(States.FlowingPlayer);
					}
				});
			
			// 设置起始状态
			FSM.StartState(States.FlowingPlayer);
		}

		private void FixedUpdate()
		{
			// 把fsm的fixupdate传入，这样fsm里的fixedupdate就会持续执行，则可以在上面通过fsm状态来调用fixedupfate
			FSM.FixedUpdate();
		}

		private void Update()
		{
			FSM.Update();
			
			if (HP<=0)
			{
				//UIKit.OpenPanel<UIGamePassPanel>();
				// TODO：添加经验值调用
				// print(gameObject.name);
				Global.GeneratePowerUp(gameObject,true);
				this.DestroyGameObjGracefully();
				//Global.Exp.Value++;
			}
		}


		private bool mIgnoreHurt = false;
		public void Hurt(float value, bool force = false,bool cirtical=false)
		{
			
			if (mIgnoreHurt&&!force)
			{
				return;
			}
			
			// toString后面输入“0”代表取整，类似excel中的的round()
			FloatingTextController.Play(transform.position+Vector3.up*1,value.ToString("0"),cirtical);
			
			Sprite.color=Color.red;
			AudioKit.PlaySound("hit");
			ActionKit.Delay(0.4f,() =>
			{
				// 敌人的血量每次减少的数量取决于主角当前的攻击力
				this.HP -= value;
				this.Sprite.color = Color.white;
				mIgnoreHurt = false;
			}).Start(this);
		}

		public void SetSpeedScale(float speedScale)
		{
			MovementSpeed *= speedScale;
		}

		public void SetHpScale(float hpScale)
		{
			HP *= hpScale;
		}
	}
}
